package kliment.display.draw.shape {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import kliment.display.draw.DrawStyle;
	import kliment.display.draw.style.FillColorStyle;
	
	import kliment.math.ColorSpaceMath;
	import kliment.geom.color.Color;
	
	public class ColorWheelBitmap extends Bitmap {
		private var _radius:Number;
		
		public function ColorWheelBitmap(center:Point, radius:Number){
			super();
			_radius = radius;
			
			_draw();
			x = center.x - radius;
			y = center.y - radius;
		}
		
		override public function get bitmapData():BitmapData { return super.bitmapData }
		override public function set bitmapData(value:BitmapData):void {
			return;
		}
		
		private function _draw():void {
			var pX:Number;
			var pY:Number = 0;
			var pXc:Number;
			var pYc:Number;
			var inRound:Number;
			var cp:Number;
			var diametr:Number = _radius * 2;
			var color:Color = new Color();
			
			var wheel:BitmapData = new BitmapData(diametr, diametr, true, 0xffffff);
			var shape:CircleShape = new CircleShape(new Point(_radius+.5, _radius+.5), _radius-.5, new DrawStyle(new FillColorStyle(0xffffff, 1)));
			wheel.draw(shape);
			
			while (pY++ < diametr) {
				pX = 0;
				while (pX++ < diametr) {
					pXc = pX - _radius;
					pYc = pY - _radius;
					inRound = Math.sqrt(pXc * pXc + pYc * pYc);
					if (inRound > diametr / 2) 
						continue;
					cp = (Math.atan2(pYc, pXc) / Math.PI * 180 + 360) % 360;
					color.hsv.setRawValue(ColorSpaceMath.artisticToScientificSmooth(360 - cp) / 360, inRound / _radius, 1);
					wheel.setPixel(pX, pY, color.hex);
				}
			}
			
			super.bitmapData = wheel;
		}
	}
}